﻿Public Class FEditContract
    Private mContract As Entities.Backup.IssuedContract

    Public Shadows Sub ShowDialog(ByVal owner As IWin32Window, ByVal contract As Entities.Backup.IssuedContract)
        Me.Icon = My.Resources.harddisc_xp
        mContract = contract
        Dim clsBuddy As Entities.Buddy = Framework.Buddies.Manager.GetById(contract.IssuedToBuddyId)
        txtBuddy.Text = clsBuddy.NameLocal
        txtFolder.Text = contract.StoragePath
        txtComment.Text = contract.Comment
        chkRevoked.Checked = contract.Revoked

        Dim lSize As Long = contract.StorageSize \ 1024
        cboMultiplier.SelectedIndex = 0
        While cboMultiplier.SelectedIndex < 4 And lSize > 1024
            lSize = lSize \ 1024
            cboMultiplier.SelectedIndex += 1
        End While
        NumericUpDown1.Value = lSize

        MyBase.ShowDialog(owner)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim clsEP As New Framework.ErrorHandling.ErrorProviderEx(ErrorProvider1)

            Dim lSize As Long = Long.MaxValue
            lSize = CInt(NumericUpDown1.Value) * GetMultiplier()
            If Not IO.Directory.Exists(txtFolder.Text) Then
                clsEP.AddError(txtFolder, "Directory does not exist!")
            ElseIf Framework.Win32.GetFreeDiskSpace(txtFolder.Text) < lSize Then
                clsEP.AddError(txtFolder, "Not enough free disk space on volume!")
            End If
            If clsEP.Count = 0 Then
                mContract.Comment = txtComment.Text
                mContract.StoragePath = txtFolder.Text
                mContract.StorageSize = lSize
                mContract.Revoked = chkRevoked.Checked
                EntityHandlers.Backup.IssuedContracts.Save(mContract)
                Me.Close()
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub
Private Function GetMultiplier() As Long
    Return CLng(1024 ^ (cboMultiplier.SelectedIndex + 1))
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop ' Environment.SpecialFolder.MyComputer

    Dim strCurrentPath As String = txtFolder.Text
    While Not IO.Directory.Exists(strCurrentPath) And strCurrentPath.Length > 0
        strCurrentPath = IO.Path.GetDirectoryName(strCurrentPath)
    End While
    FolderBrowserDialog1.SelectedPath = strCurrentPath
    FolderBrowserDialog1.ShowNewFolderButton = True
    FolderBrowserDialog1.Description = "Select folder to store backup in"

    Select Case FolderBrowserDialog1.ShowDialog(Me)
        Case Windows.Forms.DialogResult.OK
            txtFolder.Text = FolderBrowserDialog1.SelectedPath
    End Select
End Sub
End Class